<!-- @author zhengjie -->
<template>
  <div class="icon-body">
    <el-input v-model="name" clearable placeholder="请输入图标名称" style="position: relative;" @clear="filterIcons"
              @input.native="filterIcons">
      <i slot="suffix" class="el-icon-search el-input__icon"/>
    </el-input>
    <div class="icon-list">
      <div v-for="(item, index) in allIconNames" :key="index" @click="selectedIcon(item)">
        <svg-icon :icon-class="item" style="height: 30px;width: 16px;"/>
        <span class="icon-span">{{ item }}</span>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
let name = ref("")
let iconList = ref([])
// 所有的图标名称集合
const allIconNames: string[] = [];
//@ts-ignore
const icons = import.meta.glob("@/assets/icons/svg/*.svg");
for (const icon in icons) {
  const iconName = icon.split("assets/icons/svg/")[1].split(".svg")[0];
  allIconNames.push(iconName)
}
const filterIcons = (() => {
  iconList.value = icons
  if (name.value) {
    //@ts-ignore
    iconList.value = iconList.value.filter(item => item.includes(name.value))
  }
})

const emits = defineEmits(["selected"])
const selectedIcon = ((name) => {
  emits('selected', name)
  document.body.click()
})
const reset = (() => {
  name.value = ''
  iconList.value = icons
})
</script>

<script lang="ts">


export default {
  name: 'IconSelect'
}
</script>

<style lang="scss" rel="stylesheet/scss" scoped>
.icon-body {
  width: 100%;
  padding: 10px;

  .icon-list {
    height: 200px;
    overflow-y: scroll;

    div {
      height: 30px;
      line-height: 30px;
      margin-bottom: -5px;
      cursor: pointer;
      width: 33%;
      float: left;
    }

    span {
      display: inline-block;
      vertical-align: -0.15em;
      fill: currentColor;
      overflow: hidden;
    }
  }
}

.svg-icon {
  fill: #606266;
}

.icon-span {
  margin: 0 0 -6px 5px;
}
</style>
